﻿using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model;
using HengTong.Model.Db.Hr;


namespace HengTong.ViewModel.Hr.SysHrInterviewPoolVms
{
    public partial class SysHrInterviewPoolListVm : BasePagedListVM<SysHrInterviewPoolView, SysHrInterviewPoolSearcher>
    {
        protected override IEnumerable<IGridColumn<SysHrInterviewPoolView>> InitGridHeader()
        {
            return new List<GridColumn<SysHrInterviewPoolView>>
            {
                // 候选池基本信息
                this.MakeGridHeader(x => x.BillNo),
                this.MakeGridHeader(x => x.HiredStaffNo),
                
                this.MakeGridHeader(x => x.HrStatus),
                this.MakeGridHeader(x => x.InterviewRequestBillName),

                // 个人基本信息
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.StaffNo),
                this.MakeGridHeader(x => x.Gender),
                this.MakeGridHeader(x => x.Age),
                this.MakeGridHeader(x => x.BirthDate),
                this.MakeGridHeader(x => x.Phone),
                this.MakeGridHeader(x => x.Email),
                this.MakeGridHeader(x => x.IdCardNo),
                this.MakeGridHeader(x => x.Ethnicity),
                this.MakeGridHeader(x => x.HighestEducation),
                this.MakeGridHeader(x => x.MaritalStatus),
                this.MakeGridHeader(x => x.PhotoId).SetFormat(PhotoIdFormat),
                this.MakeGridHeader(x => x.HomeAddress),
                this.MakeGridHeader(x => x.HouseholdRegistration),
                this.MakeGridHeader(x => x.NativePlace),
                this.MakeGridHeader(x => x.OrgName),

                // 工作相关信息
                this.MakeGridHeader(x => x.RecruitPublishName),
                this.MakeGridHeader(x => x.WorkExperience),
                this.MakeGridHeader(x => x.CurrentSalary),
                this.MakeGridHeader(x => x.ExpectedSalary),
                this.MakeGridHeader(x => x.ArrivalDate),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        private List<ColumnFormatInfo> PhotoIdFormat(SysHrInterviewPoolView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button, entity.PhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button, entity.PhotoId, 640, 480),
            };
        }

        public override IOrderedQueryable<SysHrInterviewPoolView> GetSearchQuery()
        {
            var query = DC.Set<SysHrInterviewPool>()
                .CheckEqual(Searcher.InterviewRequestBillId, x => x.InterviewRequestBillId)
                .CheckEqual(Searcher.HrStatus, x => x.HrStatus)
                .CheckContain(Searcher.BillNo, x => x.BillNo)
                .CheckContain(Searcher.HiredStaffNo, x => x.HiredStaffNo)
                // 个人信息查询
                .CheckContain(Searcher.Name, x => x.InterviewRequestBill.Name)
                .CheckContain(Searcher.StaffNo, x => x.InterviewRequestBill.StaffNo)
                .CheckContain(Searcher.Phone, x => x.InterviewRequestBill.Phone)
                .CheckContain(Searcher.IdCardNo, x => x.InterviewRequestBill.IdCardNo)
                .CheckContain(Searcher.Email, x => x.InterviewRequestBill.Email)
                .CheckEqual(Searcher.Gender, x => x.InterviewRequestBill.Gender)
                .CheckEqual(Searcher.Ethnicity, x => x.InterviewRequestBill.Ethnicity)
                .CheckEqual(Searcher.HighestEducation, x => x.InterviewRequestBill.HighestEducation)
                .CheckEqual(Searcher.MaritalStatus, x => x.InterviewRequestBill.MaritalStatus)
                .CheckBetween(Searcher.AgeStart, Searcher.AgeEnd, x => x.InterviewRequestBill.Age, includeMax: true)
                .CheckContain(Searcher.HomeAddress, x => x.InterviewRequestBill.HomeAddress)
                .CheckContain(Searcher.HouseholdRegistration, x => x.InterviewRequestBill.HouseholdRegistration)
                .CheckContain(Searcher.NativePlace, x => x.InterviewRequestBill.NativePlace)
                .CheckEqual(Searcher.OrgId, x => x.InterviewRequestBill.OrgId)

                // 工作信息查询
                .CheckEqual(Searcher.RecruitPublishRequestBillId,
                    x => x.InterviewRequestBill.RecruitPublishRequestBillId)
                .CheckBetween(Searcher.CurrentSalaryStart, Searcher.CurrentSalaryEnd,
                    x => x.InterviewRequestBill.CurrentSalary, includeMax: true)
                .CheckBetween(Searcher.ExpectedSalaryStart, Searcher.ExpectedSalaryEnd,
                    x => x.InterviewRequestBill.ExpectedSalary, includeMax: true)
                .CheckBetween(Searcher.WorkExperienceStart, Searcher.WorkExperienceEnd,
                    x => x.InterviewRequestBill.WorkExperience, includeMax: true)
                .CheckBetween(Searcher.ArrivalDate?.GetStartTime(), Searcher.ArrivalDate?.GetEndTime(),
                    x => x.InterviewRequestBill.ArrivalDate, includeMax: false)
                .Select(x => new SysHrInterviewPoolView
                {
                    ID = x.ID,
                    InterviewRequestBillName = x.InterviewRequestBill.BillCode,
                    HrStatus = x.HrStatus,
                    BillNo = x.BillNo,
                    HiredStaffNo = x.HiredStaffNo,
                    CreateTime = x.CreateTime,

                    // 基本信息
                    Name = x.InterviewRequestBill.Name,
                    StaffNo = x.InterviewRequestBill.StaffNo,
                    BirthDate = x.InterviewRequestBill.BirthDate,
                    Age = x.InterviewRequestBill.Age,
                    Gender = x.InterviewRequestBill.Gender,
                    Ethnicity = x.InterviewRequestBill.Ethnicity,
                    HighestEducation = x.InterviewRequestBill.HighestEducation,
                    MaritalStatus = x.InterviewRequestBill.MaritalStatus,
                    PhotoId = x.InterviewRequestBill.PhotoId,
                    IdCardNo = x.InterviewRequestBill.IdCardNo,
                    Phone = x.InterviewRequestBill.Phone,
                    Email = x.InterviewRequestBill.Email,
                    HomeAddress = x.InterviewRequestBill.HomeAddress,
                    HouseholdRegistration = x.InterviewRequestBill.HouseholdRegistration,
                    NativePlace = x.InterviewRequestBill.NativePlace,
                    OrgName = x.InterviewRequestBill.Org.Name,

                    // 工作信息
                    RecruitPublishName = x.InterviewRequestBill.RecruitPublishRequestBill.BillCode,
                    ArrivalDate = x.InterviewRequestBill.ArrivalDate,
                    CurrentSalary = x.InterviewRequestBill.CurrentSalary,
                    ExpectedSalary = x.InterviewRequestBill.ExpectedSalary,
                    WorkExperience = x.InterviewRequestBill.WorkExperience,
                })
                .OrderByDescending(x => x.CreateTime)
                .ThenBy(x => x.BillNo);
            return query;
        }
    }

    public class SysHrInterviewPoolView : SysHrInterviewPool
    {
        [Display(Name = "面试申请单号")] public String InterviewRequestBillName { get; set; }

        // 基本信息
        [Display(Name = "姓名")] public string Name { get; set; }
        [Display(Name = "工号")] public string StaffNo { get; set; }
        [Display(Name = "出生日期")] public DateTime? BirthDate { get; set; }
        [Display(Name = "年龄")] public int? Age { get; set; }
        [Display(Name = "性别")] public Gender? Gender { get; set; }
        [Display(Name = "民族")] public Ethnicity? Ethnicity { get; set; }
        [Display(Name = "最高学历")] public EducationLevel? HighestEducation { get; set; }
        [Display(Name = "婚姻状况")] public MaritalStatus? MaritalStatus { get; set; }
        [Display(Name = "照片")] public Guid? PhotoId { get; set; }
        [Display(Name = "身份证号")] public string IdCardNo { get; set; }
        [Display(Name = "手机号")] public string Phone { get; set; }
        [Display(Name = "邮箱")] public string Email { get; set; }
        [Display(Name = "家庭住址")] public string HomeAddress { get; set; }
        [Display(Name = "户口所在地")] public string HouseholdRegistration { get; set; }
        [Display(Name = "籍贯")] public string NativePlace { get; set; }
        [Display(Name = "公司名称")] public String OrgName { get; set; }

        // 工作信息
        [Display(Name = "招聘发布单号")] public string RecruitPublishName { get; set; }
        [Display(Name = "可到岗时间")] public DateTime? ArrivalDate { get; set; }
        [Display(Name = "原薪酬")] public decimal? CurrentSalary { get; set; }
        [Display(Name = "期望薪酬")] public decimal? ExpectedSalary { get; set; }
        [Display(Name = "工作年限")] public decimal? WorkExperience { get; set; }
    }
}